package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * spu操作类
 */
@RequestMapping("/admin/product")
@RestController
public class SpuInfoController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    /**
     *
     * @param category3Id
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result<Page<SpuInfo>> spuInfoPageList(
            @RequestParam(value = "category3Id",required = true) Long category3Id,
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit){
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);

        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);

        Page<SpuInfo> page1 = spuInfoService.page(spuInfoPage, wrapper);

        return Result.ok(page1);
    }

    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        boolean b = spuInfoService.bigSaveSpuInfo(spuInfo);
        return Result.ok();
    }

    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId){

        QueryWrapper<SpuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);

        List<SpuImage> list = spuImageService.list(wrapper);
        return Result.ok(list);
    }

    // http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId")Long spuId){

        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.getSpuAttrAndValue(spuId);

        return Result.ok(spuSaleAttrs);
    }
}
